<?php
error_reporting("E_ALL");ini_set("display_errors", 1);
$getdata=$_REQUEST;
if(empty($getdata))$getdata=$_POST?$_POST:$_GET;
$type=empty($getdata['type'])?'':$getdata['type'];
include("include/function.php");
header("content-Type: application/json; charset=utf-8");
$config = include("config.php");
include("include/".$config['DB_TYPE'].".class.php");
$db = new db;
$db->connect($config['DB_HOST'],$config['DB_USER'],$config['DB_PWD'],$config['DB_NAME'], $config['DB_PCONNECT']);
$sql = 'select * from hema_school where status=1';
$list = $db->get_all($sql);
$sqlarr=[];
$oldlist = [];
foreach($list as $v){
    $oldlist[$v['name']]=$v;
}

/*$str='company.txt';
$companyarray = json_decode(file_get_contents($str),true);
$company = [];
foreach($companyarray['data'] as $k=>$v){
    $company[$v['uuid']]=$v;
}*/
$companyshop=array(
    'c3c3bbe95a89429da529f43bf32ea979'=>array('name'=>'君峰餐饮','shop_id'=>'10011','applet_id'=>'10011'),
    '92c0aaed74d94fee9bb8703fe77c82a3'=>array('name'=>'吉食米餐饮','shop_id'=>'10012','applet_id'=>'10012'),
    '9d0e248330df43cd845125ad693559f3'=>array('name'=>'老船长餐饮','shop_id'=>'10004','applet_id'=>'10004'),
    'ce68df60c041433eb32eaa9315570976'=>array('name'=>'老船长餐饮','shop_id'=>'10004','applet_id'=>'10004'),
    '2bf7dfb4ade4494d8a393541da50b666'=>array('name'=>'老船长餐饮','shop_id'=>'10004','applet_id'=>'10004'),
    '89a7e14ae94b468da3f9b801f6c53742'=>array('name'=>'蜀丰源餐饮','shop_id'=>'10013','applet_id'=>'10013'),
    'd4be680aabfe47039218b553a712229f'=>array('name'=>'食康配餐','shop_id'=>'10003','applet_id'=>'10003'),
    'e7fc001fffce44ff85e14a0e28ba00f9'=>array('name'=>'铭菡餐饮','shop_id'=>'10008','applet_id'=>'10008'),
    'af518809b62f4c71ad16f58d28009950'=>array('name'=>'禾嘉餐饮','shop_id'=>'10014','applet_id'=>'10014'),
    '2efb3ed701c5449fa75e61efecb08a55'=>array('name'=>'欣航餐饮','shop_id'=>'10002','applet_id'=>'10002'),
    'a6478196a6b4471bb2a9afb98d11e7c8'=>array('name'=>'婳彬餐饮','shop_id'=>'10010','applet_id'=>'10010'),
    'e67f2aabd90547d795a81c726a92d38f'=>array('name'=>'演示供餐企业','shop_id'=>'10002','applet_id'=>'10002'),
    '6b77c243458e4881840b413fe8492c5a'=>array('name'=>'良兴鼎昊','shop_id'=>'10009','applet_id'=>'10009'),
    'd7525bbef0024100ab5c839554f5d59b'=>array('name'=>'居仁阁餐饮','shop_id'=>'10008','applet_id'=>'10008'),
    '10f7071563de428b987be6fc916d399e'=>array('name'=>'吉食来','shop_id'=>'10007','applet_id'=>'10007'),
    '82a811b655a84a1bad5d68d6f89b266d'=>array('name'=>'大光明（艾味佳）','shop_id'=>'10006','applet_id'=>'10006'),
    '21cccfdce08646d89e2b389d0bb58fc7'=>array('name'=>'中科餐饮','shop_id'=>'10005','applet_id'=>'10005'),
    'c1bbcefc191b4fe8a0cf094d4ae3e77a'=>array('name'=>'食康配餐','shop_id'=>'10003','applet_id'=>'10003'),


);

$str='school.txt';
$array = json_decode(file_get_contents($str),true);
//print_r($array);


foreach($array as $k=>$v){
    
    $create_time = !empty($v['createDate'])?strtotime($v['createDate']):time();
    $update_time = !empty($v['updateDate'])?strtotime($v['updateDate']):$create_time;
    
    $type_id =$v['unitProperties'];
    $shop_id=$companyshop[$v['providerId']]['shop_id'];
    $applet_id=$companyshop[$v['providerId']]['applet_id'];
    if(!empty($oldlist[$v['unitName']])){
        $updata=[];
        if(!empty($shop_id))$updata['shop_id']=$shop_id;
        if(!empty($applet_id))$updata['applet_id']=$applet_id;
        $upsql=[];
        foreach($updata as $k1=>$v1){
           $upsql[]="`".$k1."`='".$v1."'"; 
        }
        if($upsql){
            $sqlarr[] ="update hema_school_class set ".implode(',',$upsql)." where `school_id` = '".$oldlist[$v['unitName']]['school_id']."'";
        }
        $upsql=[];
        if(!empty($v['unitPhone']))$updata['phone']=$v['unitPhone'];
        foreach($updata as $k1=>$v1){
           $upsql[]="`".$k1."`='".$v1."'"; 
        }
        if($upsql){
            $sqlarr[] ="update hema_school set ".implode(',',$upsql)." where `school_id` = '".$oldlist[$v['unitName']]['school_id']."'";
        }
    }else{
        $sqlarr[] = "insert into hema_school(code,name,phone,shop_id,applet_id,type_id,create_time,update_time) ".
        "values('".$v['unitCode']."','".$v['unitName']."','".$v['unitPhone']."','".$shop_id."','".$applet_id."','".$type_id."','".$create_time."','".$update_time."')";
    
    }
    
}
/*

$class=array(
    1=>712,
    2=>713,
    3=>714,
    4=>715,
    5=>716,
    6=>717,
    7=>718,
    8=>719,
    9=>720,
    10=>721,
    11=>722,
    12=>723,
    13=>724,
);

foreach($class as $k=>$v){
    $sqlarr[] = "update hema_food_order_detail set class_num='".$k."' where `applet_id` = '10013' AND `food_month` = '202409' and class_id =".$v;
     $sqlarr[] = "update hema_food_order set class_num='".$k."' where `applet_id` = '10013' AND `fee_month` = '202409' and class_id =".$v;
    
}

$sql = 'select * from liunianjifenban where old_class_id=0';
$list = $db->get_all($sql);
foreach($list as $v){
    $sql = "update liunianjifenban set old_class_id='".$class[$v['old_class_name']]."',new_class_id='".$class[$v['new_class_name']]."' where id =".$v['id'];
    $db->query($sql);
}*/
/*
$sql = 'select student_id,name,class_id from hema_student where status=1';
$list = $db->get_all($sql);
foreach($list as $v){
    $sql = "update liunianjifenban set student_id='".$v['student_id']."' where student_id=0 and old_class_id =".$v['class_id']." and student_name='".$v['name']."'";
    $db->query($sql);
}


$sql = 'select * from liunianjifenban where student_id>0';
$list = $db->get_all($sql);$classids=[];
foreach($list as $v){
    $classids[$v['new_class_id']][]=$v['student_id'];
}
//print_r($classids);exit;

foreach($class as $k=>$v){
    $sqlarr[] = "update hema_student set status='0' where class_id=".$v." and student_id not in(".implode(',',$classids[$v]).")";
}*//*
$sql = 'select * from liunianjifenban where new_class_id=718';
$list = $db->get_all($sql);

foreach($list as $v){
    if($v['student_id']==0)continue;
    $sqlarr[] = "update hema_student set status=1,class_id='".$v['new_class_id']."' where student_id =".$v['student_id'];
}
$sql = 'select * from liunianjifenban where student_id>0';
$list = $db->get_all($sql);
	
foreach($list as $v){
    $class_name='2019级'.$v['new_class_name'].'班(六年级'.$v['new_class_name'].'班)';
    $sql = "update hema_food_order set class_id='".$v['new_class_id']."',class_name='".$class_name."' where student_id=".$v['student_id']." and class_id =".$v['old_class_id']." AND `fee_month` = '202409'";
    $sqlarr[] =$sql;
    $sql = "update hema_food_order_detail set class_id='".$v['new_class_id']."',class_name='".$class_name."',dining_id='".$v['new_class_id']."',dining_name='".$class_name."' where student_id=".$v['student_id']." and class_id =".$v['old_class_id']." AND `food_month` = '202409'";
    //$sql = "update food_order_cancel set class_id='".$v['new_class_id']."',class_name='".$class_name."' where student_id=".$v['student_id']." and class_id =".$v['old_class_id']." AND `cancel_date` >= '2024-09-01'";
    $sqlarr[] =$sql;
    $sql = "update hema_food_order_cancel set class_id='".$v['new_class_id']."',class_name='".$class_name."' where student_id=".$v['student_id']." and class_id =".$v['old_class_id']." AND `cancel_date` >= '2024-09-01'";
    $sqlarr[] =$sql;
}*/
echo count($sqlarr)."\n";
echo implode(";\n",$sqlarr);
?>